package be.ibiiztera.competition.billard;

import java.awt.Color;
import java.util.ArrayList;

import be.ibiiztera.md.pmatrix.pushmatrix.Camera;
import be.ibiiztera.md.pmatrix.pushmatrix.CouleurOutils;
import be.ibiiztera.md.pmatrix.pushmatrix.Lumiere;
import be.ibiiztera.md.pmatrix.pushmatrix.Point3D;
import be.ibiiztera.md.pmatrix.pushmatrix.Representable;
import be.ibiiztera.md.pmatrix.pushmatrix.TColor;
import be.ibiiztera.md.pmatrix.pushmatrix.extras.Polyhedre;
import be.ibiiztera.md.pmatrix.pushmatrix.extras.TriHoleAnime;
import be.ibiiztera.md.pmatrix.pushmatrix.generator.TRISphere;
import be.ibiiztera.md.pmatrix.test.pushmatrix.newtest.TestObjet;
import be.ibiiztera.pmatrix.extras.RepresentableConteneur;

public class TestGravite2 extends TestObjet {
	int X = 2;
	int Y = 2;
	int Z = 2;
	Bille[] billes = new Bille[X * Y * Z];
	Force f = new Force();

	public void ginit() {

		for (int i = 0; i < X; i++) {
			for (int j = 0; j < Y; j++) {
				for (int k = 0; k < Z; k++) {

					billes[k * Y * X + j * X + i] = new Bille();
					billes[k * Y * X + j * X + i].position = new Point3D(
							(i - X / 2f) / 1f, (j - Y / 2f) / 1f,
							(k - Z / 2f) / 1f).mult(30);
					//billes[k * Y * X + j * X + i].vitesse = new Point3D(
					//		(i - X / 2) / 1f, (j - Y / 2) / 1f,
					//		(k - Z / 2) / 1f);
					billes[k * Y * X + j * X + i].color = new Color(1.0f * i
							/ X, 1.0f * j / Y, 1.0f * k / Z);
					billes[k * Y * X + j * X + i].masse = 1;
					billes[k * Y * X + j * X + i].attraction = 1;
					billes[k * Y * X + j * X + i].repulsion = 0;
					billes[k * Y * X + j * X + i].amortissement = 0;
				}
			}

		}
		if(billes.length==X*Y*Z+1)
		{
		billes[X * Y * Z] = new Bille();
		billes[X * Y * Z].masse = 1000000;
		billes[X * Y * Z].position = new Point3D(500, 500, 0);
		billes[X * Y * Z].color = Color.YELLOW;
		billes[X * Y * Z].attraction = 1;
		billes[X * Y * Z].repulsion = 0;
		billes[X * Y * Z].amortissement = 0;
		}
		f.configurer(billes);

	}

	public void testScene() {
		scene().clear();

		f.calculer();

		setCouleurFond(new TColor(Color.BLACK));

		RepresentableConteneur rc = new RepresentableConteneur();

		Polyhedre polyhedre = null;

		ArrayList<Point3D> list = new ArrayList<Point3D>();

		for (int i = 0; i < X * Y * Z; i++) {
			Representable r = new TRISphere(billes[i].position, f.distMin());

			((TRISphere) r).setMaxX(30);
			((TRISphere) r).setMaxY(30);

			((TRISphere) r).couleur(new TColor(billes[i].color));

			rc.add(r);

			list.add(billes[i].position);

		}

		polyhedre = new Polyhedre(list, new TColor(Color.RED));

		Camera camera = new Camera(f.centreMasse().plus(
				new Point3D(0, 0, -2 * f.getDistMax() - f.distMin())),
				f.centreMasse());

		scene().cameraActive(camera);

		scene().lumieres().add(new Lumiere() {
			private Point3D dir = new Point3D(1, 1, 1);
			private Color c = Color.WHITE;

			@Override
			public Color getCouleur(TColor base, Point3D p, Point3D n) {
				Point3D v = n.norme1().prodVect(p.norme1());

				double r = Math.abs(Math.acos(v.norme()) / Math.PI);

				return CouleurOutils.couleurRatio(base.getCouleur(), c, r);
			}
		});

		// scene().add(polyhedre);

		scene().add(rc);

	}

	public static void main(String[] args) {

		TestGravite2 ttn = new TestGravite2();

		ttn.setResx(640);
		ttn.setResy(480);
		ttn.loop(true);
		ttn.setMaxFrames(100000);
		ttn.publishResult(true);
		ttn.setFileExtension("jpg");

		ttn.run();

	}

}
